package sing.core.base

import android.app.Application
import android.widget.ProgressBar
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.LoadSir
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.wrapper.RefreshFooterWrapper
import sing.core.R
import sing.core.callback.load_sir.EmptyCallback
import sing.core.callback.load_sir.ErrorCallback
import sing.core.callback.load_sir.LoadingCallback

/**
 * 对于写BaseApp，其实是拒绝的，但是需要提供一个很有用的功能--在Activity/fragment中获取Application级别的ViewModel
 * 所以才加的，如果不想继承BaseApp，又想获取Application级别的ViewModel功能
 * 那么可以复制该类的代码到你的自定义Application中去，然后可以自己写获取ViewModel的拓展函数即 :
 * GetViewModelExt类的getAppViewModel方法
 */

open class BaseApp : Application(), ViewModelStoreOwner {

    private lateinit var mAppViewModelStore: ViewModelStore

    private var mFactory: ViewModelProvider.Factory? = null

    override fun getViewModelStore(): ViewModelStore {
        return mAppViewModelStore
    }

    override fun onCreate() {
        super.onCreate()
        initApp(this)
        mAppViewModelStore = ViewModelStore()
        app = this

        //界面加载管理 初始化
        LoadSir.beginBuilder()
            .addCallback(LoadingCallback())//加载
            .addCallback(ErrorCallback())//错误
            .addCallback(EmptyCallback())//空
            .setDefaultCallback(SuccessCallback::class.java)//设置默认加载状态页
            .commit()

        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white)//全局设置主题颜色
            MaterialHeader(context)
        }

        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
            val progressBar = ProgressBar(this)
            RefreshFooterWrapper(progressBar)
        }
    }

    companion object {

        lateinit var app: Application

        fun initApp(app: Application) {
            Companion.app = app
//
//            // 监听所有 Activity 的创建和销毁
//            app.registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
//                override fun onActivityPaused(activity: Activity) {
//                }
//
//                override fun onActivityStarted(activity: Activity) {
//                }
//
//                override fun onActivityDestroyed(activity: Activity) {
//                    AppActivityManager.remove(activity)
//                }
//
//                override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
//                }
//
//                override fun onActivityStopped(activity: Activity) {
//                }
//
//                override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
//                    AppActivityManager.add(activity)
//                }
//
//                override fun onActivityResumed(activity: Activity) {
//                }
//
//            })
        }

        fun getInstance(): Application {
            return app
        }


    }

    /**
     * 获取一个全局的ViewModel
     */
    fun getAppViewModelProvider(): ViewModelProvider {
        return ViewModelProvider(this, this.getAppFactory())
    }

    private fun getAppFactory(): ViewModelProvider.Factory {
        if (mFactory == null) {
            mFactory = ViewModelProvider.AndroidViewModelFactory.getInstance(this)
        }
        return mFactory as ViewModelProvider.Factory
    }
}